package com.infocollect.common.sms;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.infocollect.common.exception.CustomException;
import com.infocollect.project.system.domain.SysConfig;
import com.infocollect.project.system.service.ISysConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.LinkedHashMap;

/**
 * 此类描述的是： 阿里大于短信发送方法
 *@ClassName: AliMessageSender
 *@Description: 
 *@author Huang.Bin
 *@date 2019年4月23日 上午9:41:19
 *@Email: 1104725109@qq.com
 *@version V1.0
 */
@Component
public class AliMessageSender implements MessageSender {
	
	/** Logger */
	private static final Logger log = LoggerFactory.getLogger(AliMessageSender.class);
	
	private static String accessKeyId;// 你的accessKeyId,参考本文档步骤2;//(杭州)LTAIzQ0CKnpAPHla  (湖州)LTAIci4iTEydIjtH
	private static String accessKeySecret;// 你的accessKeySecret，参考本文档步骤2//(杭州)Pf1BBc83k3GLVZimqE8ZGJJxJd4ero （湖州）zj9nLpPk4F3YBYr8cupiUKXvlAMFka
	private static String product;// 短信API产品名称（短信产品名固定，无需修改）
	private static String domain;// 短信API产品域名（接口地址固定，无需修改）
	private static String signName;//短信签名
	private static String regionId;
	private static String endpointName;
	private IClientProfile profile;
	private IAcsClient acsClient;
	private static final AliMessageSender instance = new AliMessageSender();

	@Autowired
	private ISysConfigService configService;
	private AliMessageSender() {

	}
	@PostConstruct
	private void init(){
		accessKeyId=configService.selectConfigByKey("smsAccessKeyId");
		accessKeySecret=configService.selectConfigByKey("smsAccessKeySecret");
		product =configService.selectConfigByKey("smsProduct");
		domain = configService.selectConfigByKey("smsDomain");
		signName = configService.selectConfigByKey("smsSignName");
		regionId =  configService.selectConfigByKey("smsRegionId");
		endpointName = configService.selectConfigByKey("smsEndpointName");
	}
	public static AliMessageSender getInstance() {

//		accessKeyId=GlobalUtil.getValue("configService");
//		accessKeySecret=GlobalUtil.getValue("smsAccessKeySecret");
//		product=GlobalUtil.getValue("smsProduct");
//		domain=GlobalUtil.getValue("smsDomain");
//		signName=GlobalUtil.getValue("smsSignName");
//		regionId=GlobalUtil.getValue("smsRegionId");
//		endpointName=GlobalUtil.getValue("smsEndpointName");
		instance.profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
		try {
			DefaultProfile.addEndpoint(endpointName,regionId, product, domain);
		} catch (ClientException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		instance.acsClient = new DefaultAcsClient(instance.profile);
		return instance;
	}

	public boolean sendMessage(final String mobile, final String templateId, final LinkedHashMap<String, String> map)
			throws Exception {
		System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
		System.setProperty("sun.net.client.defaultReadTimeout", "10000");
		// 组装请求对象
		SendSmsRequest request = new SendSmsRequest();
		// 使用post提交
		request.setMethod(MethodType.POST);
		request.setPhoneNumbers(mobile);
		request.setSignName(signName);
		request.setTemplateCode(templateId);
		request.setTemplateParam(JSON.toJSONString(map));
		boolean result=false;
		SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
		if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
			// 请求成功
			log.info(sendSmsResponse.getMessage());
			result=true;
		}else {
			log.error(JSON.toJSONString(sendSmsResponse));
			throw new CustomException(JSON.toJSONString(sendSmsResponse));
		}
		return result;
	}

	public boolean sendVoiceMessage(String mobile, String templateId, LinkedHashMap<String, String> map) {
		return false;
	}
}
